package com.bluestone.assertor;

import org.apache.log4j.spi.LoggingEvent;

import com.bluestone.util.Util;

public class AIVRHttpFilter implements AssertorFilter{

	private String applicationState = "ApplicationState";
	private String confirmationNumber = "ConfirmationNumber";
	private String getInspectionResponse = "GetInspectionResponse";
	private String scheduleInspectionResponse = "ScheduleInspectionResponse";
	private String inspectionDate = "InspectionDate";
	private String inspectionTime = "InspectionTime";
	private String inspection = "Inspection";
	
	public boolean doFilter(Object obj) {
		if(String.class.isInstance(obj)){
			String temp = (String)obj;
			
			//save latest applicationState to para map
			String status = Util.getNodeValue(temp, applicationState);   
			if(status!=null && status.length()>0){
				Util.putDynamicPara(applicationState, status);
				Util.getLogger().info("AIVRHttpFilter:put dynamic para "+applicationState+":"+status);
			}
			
			// save latest confirmation to para map
			String number = Util.getNodeValue(temp, confirmationNumber);
			if(number!=null && number.length()>0){
				Util.putDynamicPara(confirmationNumber, number);
				Util.getLogger().info("AIVRHttpFilter:put dynamic para "+confirmationNumber+":"+number);
			}	
			
			//save latest inspectiondate to para map
			String inspdate = Util.getNodeValue(temp, inspectionDate);
			if(inspdate!=null && inspdate.length()>0){
				Util.putDynamicPara("inspectionDate", inspdate);
				Util.getLogger().info("AIVRHttpFilter:put dynamic para inspectionDate:"+":"+inspdate);
			}
			
			//save latest inspectiondate to para map
			String insptime = Util.getNodeValue(temp, inspectionTime);
			if(insptime!=null && insptime.length()>0){
				Util.putDynamicPara("inspectionTime", insptime);
				Util.getLogger().info("AIVRHttpFilter:put dynamic para inspectionTime:"+":"+insptime);
			}
			
			
			// get inspection, and save to para map, it's for reschedule
			String inspectionData = Util.getNodeValue(temp, getInspectionResponse);
			if(inspectionData!=null && inspectionData.length()>0){
				inspectionData = Util.getNodeValue(inspectionData, inspection);
				int pos1 = inspectionData.indexOf("<InspectionDate>");
				int pos2 = inspectionData.indexOf("</InspectionDate>");
				String str1 = "<InspectionDate>{inspectionDate}</InspectionDate>";
				inspectionData = inspectionData.substring(0,pos1) + str1 + inspectionData.substring(pos2+ "</InspectionDate>".length());
				
				String str2 = "<InspectionTime>{inspectionTime}</InspectionTime>";
				pos1 = inspectionData.indexOf("<InspectionTime>");
				pos2 = inspectionData.indexOf("</InspectionTime>");	
				inspectionData = inspectionData.substring(0,pos1) + str2 + inspectionData.substring(pos2+ "</InspectionTime>".length());
				
				Util.putDynamicPara("currentInspection", inspectionData);
				Util.getLogger().info("AIVRHttpFilter:put dynamic para currentInspection"+":"+inspectionData);
				
			}
			
			return true;
	    }
		return false;		
	}
	


}
